<body>
<p>Test SharedWorker event listener. Should print a series of PASS messages, followed with DONE.</p>
<div id=result></div>
<script src="../../resources/gc.js"></script>
<script>
function log(message)
{
    document.getElementById("result").innerHTML += message + "<br>";
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var worker = new SharedWorker("resources/worker-common.js", "foobar");
var numListenerCalls = 0;
function countListenerCallbacks(event)
{
    if (event.type != "foo")
        log("FAIL: Received unexpected event: " + event.type);
    numListenerCalls++;
}
worker.addEventListener("foo", countListenerCallbacks, false);
var event = document.createEvent("Events");
event.initEvent("foo", false, false);
worker.dispatchEvent(event);
if (numListenerCalls == 1) {
    log("SUCCESS: Listener was called.");
} else {
    log("FAIL: Listener was not called.");
}

worker.removeEventListener("foo", countListenerCallbacks, false);
worker.dispatchEvent(event);
if (numListenerCalls == 1) {
    log("SUCCESS: Listener was not called after removal.");
} else {
    log("FAIL: Listener was called after removal.");
}

// Test to make sure that event listeners survive GC.
worker.addEventListener("foo", createNewListener(countListenerCallbacks), false);
gc();
worker.dispatchEvent(event);
if (numListenerCalls == 2) {
    log("SUCCESS: Listener was called after GC.");
} else {
    log("FAIL: Listener was not called after GC.");
}


log("DONE");
if (window.testRunner)
    testRunner.notifyDone();

function createNewListener(func)
{
  return function(event) { func(event); };
}

</script>
</body>
